<?php
    $this->layout('reading.phtml', [
        'title' => _('News'),
        'canonical' => url_full('news'),
        'current_tab' => 'reading',
        'current_page' => 'news',
        'reset_back_options' => true,
    ]);
?>

<div class="section__title">
    <h1><?= _('News') ?></h1>
</div>

<?php if (!$links_timeline->empty()): ?>
    <p class="collection__number-links">
        <?php $count_links = $links_timeline->count(); ?>
        <?= _nf('%s link', '%s links', $count_links, format_number($count_links)) ?>
    </p>

    <div data-controller="zindex-inverser">
        <?php foreach ($links_timeline->datesGroups() as $date_group): ?>
            <section class="group">
                <header class="group__header" data-zindex-inverser-target="item">
                    <h2 class="group__title">
                        <?= _date($date_group->date, 'dd MMMM') ?>

                        <?php if ($date_group->isToday()): ?>
                            <small class="text--secondary">⋅ <?= _('today') ?></small>
                        <?php elseif ($date_group->isYesterday()): ?>
                            <small class="text--secondary">⋅ <?= _('yesterday') ?></small>
                        <?php endif; ?>
                    </h2>

                    <div class="group__separator"></div>

                    <details
                        class="popup"
                        data-controller="popup"
                        data-action="toggle->popup#update click@window->popup#closeOnClickOutside keydown->popup#closeOnEscape"
                    >
                        <summary class="popup__opener">
                            <span class="button button--small button--ghost">
                                <?= icon('menu') ?>

                                <span class="no-mobile">
                                    <?= _('Actions') ?>
                                </span>
                            </span>
                        </summary>

                        <nav class="popup__container popup__container--left" role="menu">
                            <div class="popup__title"><?= _('Actions on the day') ?></div>

                            <form method="post" action="<?= url('mark collection as read', ['id' => $news->id]) ?>" role="menuitem">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= url('news') ?>" />
                                <input type="hidden" name="date" value="<?= $date_group->date->format('Y-m-d') ?>" />

                                <button class="popup__item popup__item--button">
                                    <?= icon('check') ?>
                                    <?= _('Mark all as read') ?>
                                </button>
                            </form>

                            <form method="post" action="<?= url('read collection later', ['id' => $news->id]) ?>" role="menuitem">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= url('news') ?>" />
                                <input type="hidden" name="date" value="<?= $date_group->date->format('Y-m-d') ?>" />

                                <button class="popup__item popup__item--button">
                                    <?= icon('bookmark') ?>
                                    <?= _('Read the links later') ?>
                                </button>
                            </form>

                            <div class="popup__separator"></div>

                            <form
                                method="post"
                                action="<?= url('never read collection', ['id' => $news->id]) ?>"
                                data-turbo-confirm="<?= _('You’ll remove all the news links, this action cannot be canceled. Are you sure?') ?>"
                                role="menuitem"
                            >
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= url('news') ?>" />
                                <input type="hidden" name="date" value="<?= $date_group->date->format('Y-m-d') ?>" />

                                <button class="popup__item popup__item--button">
                                    <?= icon('times') ?>
                                    <?= _('Remove the links from the news') ?>
                                </button>
                            </form>
                        </nav>
                    </details>
                </header>

                <div class="cards">
                    <?php foreach ($date_group->links as $link): ?>
                        <?= $this->include('links/_link.phtml', [
                            'link' => $link,
                            'from' => \Minz\Url::for('news'),
                            'display_edit' => true,
                            'display_repair' => true,
                            'display_source' => true,
                            'display_read_later' => true,
                            'display_mark_as_read' => true,
                            'display_never' => true,
                            'storing_must_mark_as_read' => true,
                        ]); ?>
                    <?php endforeach; ?>
                </div>

                <?php foreach ($date_group->sourceGroups() as $source_group): ?>
                    <div class="news__source-group">
                        <div class="line">
                            <h3 class="news__source-title">
                                <?= protect($source_group->title) ?>

                                <small class="text--secondary">
                                    ⋅ <?= _nf('%s link', '%s links', count($source_group->links), count($source_group->links)) ?>
                                </small>
                            </h3>

                            <details
                                class="popup"
                                data-controller="popup"
                                data-action="toggle->popup#update click@window->popup#closeOnClickOutside keydown->popup#closeOnEscape"
                            >
                                <summary class="popup__opener">
                                    <span class="button button--smaller button--ghost">
                                        <?= icon('menu') ?>

                                        <span class="sr-only">
                                            <?= _('Actions') ?>
                                        </span>
                                    </span>
                                </summary>

                                <nav class="popup__container popup__container--left" role="menu">
                                    <div class="popup__title"><?= _('Actions on the feed') ?></div>

                                    <form data-turbo-preserve-scroll method="post" action="<?= url('mark collection as read', ['id' => $news->id]) ?>" role="menuitem">
                                        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                        <input type="hidden" name="from" value="<?= url('news') ?>" />
                                        <input type="hidden" name="date" value="<?= $date_group->date->format('Y-m-d') ?>" />
                                        <input type="hidden" name="source" value="<?= $source_group->reference ?>" />

                                        <button class="popup__item popup__item--button">
                                            <?= icon('check') ?>
                                            <?= _('Mark all as read') ?>
                                        </button>
                                    </form>

                                    <form data-turbo-preserve-scroll method="post" action="<?= url('read collection later', ['id' => $news->id]) ?>" role="menuitem">
                                        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                        <input type="hidden" name="from" value="<?= url('news') ?>" />
                                        <input type="hidden" name="date" value="<?= $date_group->date->format('Y-m-d') ?>" />
                                        <input type="hidden" name="source" value="<?= $source_group->reference ?>" />

                                        <button class="popup__item popup__item--button">
                                            <?= icon('bookmark') ?>
                                            <?= _('Read the links later') ?>
                                        </button>
                                    </form>

                                    <div class="popup__separator"></div>

                                    <form
                                        data-turbo-preserve-scroll
                                        method="post"
                                        action="<?= url('never read collection', ['id' => $news->id]) ?>"
                                        data-turbo-confirm="<?= _('You’ll remove all the news links, this action cannot be canceled. Are you sure?') ?>"
                                        role="menuitem"
                                    >
                                        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                        <input type="hidden" name="from" value="<?= url('news') ?>" />
                                        <input type="hidden" name="date" value="<?= $date_group->date->format('Y-m-d') ?>" />
                                        <input type="hidden" name="source" value="<?= $source_group->reference ?>" />

                                        <button class="popup__item popup__item--button">
                                            <?= icon('times') ?>
                                            <?= _('Remove the links from the news') ?>
                                        </button>
                                    </form>
                                </nav>
                            </details>
                        </div>

                        <div class="cards">
                            <?php foreach ($source_group->links as $link): ?>
                                <?= $this->include('links/_link.phtml', [
                                    'link' => $link,
                                    'from' => \Minz\Url::for('news'),
                                    'display_edit' => true,
                                    'display_repair' => true,
                                    'display_source' => true,
                                    'display_read_later' => true,
                                    'display_mark_as_read' => true,
                                    'display_never' => true,
                                    'storing_must_mark_as_read' => true,
                                ]); ?>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </section>
        <?php endforeach; ?>
    </div>

    <div class="news__postpone">
        <details
            class="popup news__postpone-popup"
            data-controller="popup"
            data-action="toggle->popup#update click@window->popup#closeOnClickOutside keydown->popup#closeOnEscape"
        >
            <summary class="popup__opener">
                <span class="button button--primary news__postpone-button">
                    <?= icon('menu') ?>
                    <?= _('Actions') ?>
                </span>
            </summary>

            <nav class="popup__container popup__container--bottom" role="menu">
                <div class="popup__title"><?= _('Empty the news') ?></div>

                <form method="post" action="<?= url('mark collection as read', ['id' => $news->id]) ?>" role="menuitem">
                    <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                    <input type="hidden" name="from" value="<?= url('news') ?>" />

                    <button class="popup__item popup__item--button">
                        <?= icon('check') ?>
                        <?= _('Mark all as read') ?>
                    </button>
                </form>

                <form method="post" action="<?= url('read collection later', ['id' => $news->id]) ?>" role="menuitem">
                    <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                    <input type="hidden" name="from" value="<?= url('news') ?>" />

                    <button class="popup__item popup__item--button">
                        <?= icon('bookmark') ?>
                        <?= _('Read the links later') ?>
                    </button>
                </form>

                <div class="popup__separator"></div>

                <form
                    method="post"
                    action="<?= url('never read collection', ['id' => $news->id]) ?>"
                    data-turbo-confirm="<?= _('You’ll remove all the news links, this action cannot be canceled. Are you sure?') ?>"
                    role="menuitem"
                >
                    <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                    <input type="hidden" name="from" value="<?= url('news') ?>" />

                    <button class="popup__item popup__item--button">
                        <?= icon('times') ?>
                        <?= _('Remove the links from the news') ?>
                    </button>
                </form>
            </nav>
        </details>

        <p class="wrapper wrapper--small wrapper--center text--secondary">
            <?= _('Note: you will need to clear the news before you can refresh it. Mark the links as read, to be read later, or remove them with the appropriate actions.') ?>
        </p>
    </div>
<?php else: ?>
    <p class="section__intro">
        <?= _('Fill your news feed with the content published by the feeds that you follow.') ?>
    </p>

    <div class="wrapper wrapper--small wrapper--center">
        <?php if ($no_news): ?>
            <?= $this->include('alerts/_info.phtml', [
                'message' => _('There are no relevant links to suggest at this time.'),
            ]) ?>
        <?php endif; ?>

        <form
            method="post"
            action="<?= url('fill news') ?>"
            data-controller="news-refresher"
            data-news-refresher-url-value="<?= url('news available') ?>"
        >
            <?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />

            <div class="text--centered">
                <button
                    class="news__refresh-button"
                    data-news-refresher-target="button"
                >
                    <?= icon('sync') ?>
                    <?= _('Refresh the news') ?>
                </button>
            </div>
        </form>
    </div>
<?php endif; ?>

<img
    class="illustration illustration--centered"
    alt=""
    src="<?= url_static('illustrations/explore.svg') ?>"
    height="250"
/>
